#!/usr/bin/env bash

OS=`uname -s`

SCRIPT_DIR=`echo $0 | sed -e 's/[^\/]*$//'`

pushd $SCRIPT_DIR > /dev/null

SCRIPT_DIR=`pwd`
SFILE=$SCRIPT_DIR/ghidraSvr

if [ ! -x $SFILE ]; then
	CHECK_FILE_PERM=`file $SFILE | grep 'Permission'`
	if [ "$CHECK_FILE_PERM" != "" ]; then
        	echo "Ghidra Server file permissions prevent installation (see svrREADME.html)";
	elif [ -e $SFILE ]; then
        	echo "Ghidra Server startup script $SFILE must be executable!";
	else
        	echo "Ghidra Server startup script $SFILE not found!";
	fi
	exit 1
fi

success=1;
if [ "$OS" = "Linux" ]; then
    $SFILE install
	success=$?
elif [ "$OS" = "Darwin" ]; then
    $SFILE install
	success=$?
fi

popd > /dev/null

if [ $success -eq 0 ]; then
	echo "Successfully installed Ghidra Server."
	$SFILE start
	exit $?
else
	echo "Failed to install Ghidra Server!"
	exit 1
fi

